import {
  Controller,
  Get,
  Post,
  Put,
  Delete,
  Body,
  Param,
  Query,
  UseGuards,
  UseInterceptors,
  UploadedFile,
  UploadedFiles,
  Session,
  Req,
} from '@nestjs/common';
import { FileInterceptor, FilesInterceptor } from '@nestjs/platform-express';
import {
  ApiTags,
  ApiOperation,
  ApiResponse,
  ApiConsumes,
} from '@nestjs/swagger';
import { Request } from 'express';
import { JwtAuthGuard } from '@wwjCommon/security/guards/jwt-auth.guard';
import { RolesGuard } from '@wwjCommon/security/guards/roles.guard';
import { Roles } from '@wwjCommon/security/decorators/roles.decorator';
import { Public } from '@wwjCommon/security/decorators/public.decorator';
import { BusinessException } from '@wwjCommon/exception/business.exception';
// @UploadedFile() - 单文件上传，配合 @UseInterceptors(FileInterceptor('file'))
// @UploadedFiles() - 多文件上传，配合 @UseInterceptors(FilesInterceptor('files'))
// @Session() - 获取会话对象，对应PHP Session::get()
// @Req() - 获取Request对象，对应PHP Request
import { SiteGroupService } from '../../services/admin/site-group.service';

/**
 * SiteGroupController
 * 对应 PHP: SiteGroup Controller
 * 对应 Java: @RestController
 *
 * 支持装饰器：
 * - @UploadedFile() - 单文件上传 (对应PHP Request::file())
 * - @UploadedFiles() - 多文件上传
 * - @Session() - 会话管理 (对应PHP Session::get())
 * - @Req() - 请求对象 (对应PHP Request)
 */
@ApiTags('site')
@Controller('adminapi/site')
export class SiteGroupController {
  constructor(private readonly siteGroupService: SiteGroupService) {}

  /**
   * 站点分组
   * 路由: GET group
   * PHP路由: Route::get('group', 'site.SiteGroup/lists')
   */
  @Get('group')
  @UseGuards(JwtAuthGuard, RolesGuard)
  @ApiOperation({ summary: '站点分组' })
  async lists(): Promise<ApiResponse> {
    try {
      // 基于PHP真实逻辑实现
      // PHP原始方法: lists

      return await this.siteGroupService.getPage(data);
    } catch (error) {
      throw new BusinessException('lists操作失败', error);
    }
  }

  /**
   * 站点分组
   * 路由: GET group/:group_id
   * PHP路由: Route::get('group/:group_id', 'site.SiteGroup/info')
   */
  @Get('group/:group_id')
  @UseGuards(JwtAuthGuard, RolesGuard)
  @ApiOperation({ summary: '站点分组' })
  async info(@Param('group_id') group_id: string): Promise<ApiResponse> {
    try {
      // 基于PHP真实逻辑实现
      // PHP原始方法: info

      return await this.siteGroupService.getInfo(group_id);
    } catch (error) {
      throw new BusinessException('info操作失败', error);
    }
  }

  /**
   * 站点分组
   * 路由: POST group
   * PHP路由: Route::post('group', 'site.SiteGroup/add')
   */
  @Post('group')
  @UseGuards(JwtAuthGuard, RolesGuard)
  @ApiOperation({ summary: '站点分组' })
  async add(@Body() data: AddDto): Promise<ApiResponse> {
    try {
      // 基于PHP真实逻辑实现
      // PHP原始方法: add

      return await this.siteGroupService.add(data);
    } catch (error) {
      throw new BusinessException('add操作失败', error);
    }
  }

  /**
   * 站点分组
   * 路由: PUT group/:group_id
   * PHP路由: Route::put('group/:group_id', 'site.SiteGroup/edit')
   */
  @Put('group/:group_id')
  @UseGuards(JwtAuthGuard, RolesGuard)
  @ApiOperation({ summary: '站点分组' })
  async edit(
    @Param('group_id') group_id: string,
    @Body() data: EditDto,
  ): Promise<ApiResponse> {
    try {
      // 基于PHP真实逻辑实现
      // PHP原始方法: edit

      return await this.siteGroupService.edit(group_id, data);
    } catch (error) {
      throw new BusinessException('edit操作失败', error);
    }
  }

  /**
   * 站点分组
   * 路由: DELETE group/:group_id
   * PHP路由: Route::delete('group/:group_id', 'site.SiteGroup/del')
   */
  @Delete('group/:group_id')
  @UseGuards(JwtAuthGuard, RolesGuard)
  @ApiOperation({ summary: '站点分组' })
  async del(@Param('group_id') group_id: string): Promise<ApiResponse> {
    try {
      // 基于PHP真实逻辑实现
      // PHP原始方法: del

      return await this.siteGroupService.del(group_id);
    } catch (error) {
      throw new BusinessException('del操作失败', error);
    }
  }

  /**
   * 站点分组
   * 路由: GET group/all
   * PHP路由: Route::get('group/all', 'site.SiteGroup/all')
   */
  @Get('group/all')
  @UseGuards(JwtAuthGuard, RolesGuard)
  @ApiOperation({ summary: '站点分组' })
  async all(): Promise<ApiResponse> {
    try {
      // 基于PHP真实逻辑实现
      // PHP原始方法: all

      return await this.siteGroupService.getAll();
    } catch (error) {
      throw new BusinessException('all操作失败', error);
    }
  }

  /**
   * 站点分组
   * 路由: GET group/user
   * PHP路由: Route::get('group/user', 'site.SiteGroup/getUserSiteGroupAll')
   */
  @Get('group/user')
  @UseGuards(JwtAuthGuard, RolesGuard)
  @ApiOperation({ summary: '站点分组' })
  async getUserSiteGroupAll(): Promise<ApiResponse> {
    try {
      // 基于PHP真实逻辑实现
      // PHP原始方法: getUserSiteGroupAll

      return await this.siteGroupService.getUserSiteGroupAll(data['uid']);
    } catch (error) {
      throw new BusinessException('getUserSiteGroupAll操作失败', error);
    }
  }
}
